import { defineStore } from "pinia";
import { getPersonInfo } from "@/api";
import { jwtDecode } from "jwt-decode";
export const useUserStore = defineStore("user", {
  persist: false,
  state: () => {
    return {
      userInfo: {
        id: -1,
        createTime: "2021-02-24 21:16:41",
        updateTime: "2023-12-22 16:29:50",
        departmentId: "",
        username: "",
        passwordV: 10,
        nickName: "",
        headImg: "",
        avatar: "",
        phone: "",
        email: "",
        status: 1,
        storageSize: 0,
      },
      roles: [],
    };
  },
  getters: {},
  actions: {
    async updateUserInfo() {
      const res: any = await getPersonInfo();
      this.userInfo = res.data;
      const token = localStorage.getItem("token");
      const decoded = jwtDecode(token);
      this.roles = decoded["roleIds"];
      console.log("roles", this.roles);
      return this.userInfo;
    },
    clearUserInfo() {
      this.userInfo = {
        id: 1,
        createTime: "2021-02-24 21:16:41",
        updateTime: "2023-12-22 16:29:50",
        departmentId: "",
        name: "",
        username: "",
        passwordV: 10,
        nickName: "",
        headImg: "",
        phone: "",
        email: "",
        remark: "",
        status: 1,
        socketId: null,
      };
      localStorage.clear();
    },
  },
});
